﻿using Core.Domain.Entities;
using Finbuckle.MultiTenant;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Persistence.Configurations;

public class PermissionConfiguration : IEntityTypeConfiguration<Permission>
{
    public void Configure(EntityTypeBuilder<Permission> builder)
    {
        // 设置表名（可选）
        builder
            .ToTable("Permissions", "identity")
            .IsMultiTenant();

        // 配置主键
        builder.HasKey(p => p.Id); // 假设 Code 是业务主键

        // 配置常规属性
        builder.Property(p => p.Code)
               .IsRequired(false)
               .HasMaxLength(50)
               .HasColumnName("Code"); // 大驼峰命名

        builder.Property(p => p.Name)
               .IsRequired(false)
               .HasMaxLength(100)
               .HasColumnName("Name");

        builder.Property(p => p.Group)
               .HasMaxLength(50)
               .HasColumnName("Group");

        builder.Property(o => o.ParentId)
            .IsRequired(false);

        // 自引用关系：Parent ←→ Children
        builder.HasOne(o => o.Parent)                    // 当前组织的父级
               .WithMany()
               .HasForeignKey(o => o.ParentId)
               .OnDelete(DeleteBehavior.Restrict);

        builder.Property(rp => rp.Scope)
               .HasConversion<string>()
               .HasMaxLength(256)
               .IsRequired(false);

        //// 配置 MenuMeta 值对象（嵌套记录）
        builder.Property(o => o.MenuType)
            .HasDefaultValue(0)
           .IsRequired(false);

        builder.Property(o => o.Title)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.RouteName)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.Path)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.Component)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.Rank)
            .HasDefaultValue(99)
           .IsRequired(false);

        builder.Property(o => o.Redirect)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.Icon)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.ExtraIcon)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.EnterTransition)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.LeaveTransition)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.ActivePath)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.FrameSrc)
            .HasMaxLength(100)
           .IsRequired(false);

        builder.Property(o => o.FrameLoading)
            .HasDefaultValue(true)
           .IsRequired(false);

        builder.Property(o => o.KeepAlive)
            .HasDefaultValue(false)
           .IsRequired(false);

        builder.Property(o => o.HiddenTag)
            .HasDefaultValue(false)
           .IsRequired(false);

        builder.Property(o => o.FixedTag)
            .HasDefaultValue(false)
            .IsRequired(false);

        builder.Property(o => o.ShowLink)
            .HasDefaultValue(false)
           .IsRequired(false);


        builder.Property(o => o.ShowParent)
            .HasDefaultValue(false)
          .IsRequired(false);




        // 索引配置（可选）：提高查询性能
        builder.HasIndex(p => p.Name).HasDatabaseName("IX_Permissions_Name");
        builder.HasIndex(p => p.Group).HasDatabaseName("IX_Permissions_Group");
        // 索引优化
        builder.HasIndex(p => p.ParentId).HasDatabaseName("IX_Permissions_ParentId");
    }
}
